package com.ilxqx.fierce.action;

import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ilxqx.fierce.core.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 获取所有数据处理器
 *
 * @author Venus
 * @since 1.0.0
 */
@Component
public class GetListActionHandler implements CommonActionHandler {
    /**
     * 处理动作
     *
     * @param action      动作
     * @param requestData 请求数据
     * @return 响应对象
     */
    @Override
    public FierceResponse handle(String action, Map<String, Object> requestData) {
        FierceRequestContext context = FierceRequestContextHolder.get();

        List<?> objects = ReflectUtil.invoke(
            context.getMapper(),
            ReflectUtil.getMethod(BaseMapper.class, "selectList", Wrapper.class),
            new Object[]{null}
        );

        return FierceResponse.succeed(objects);
    }

    /**
     * 是否支持
     *
     * @param candidate 候选者
     * @return bool
     */
    @Override
    public boolean supports(String candidate) {
        return CommonAction.GET_LIST.getAction().equals(candidate);
    }
}
